package com.zs.netty.live;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.internal.StringUtil;

import java.nio.charset.StandardCharsets;

/**
 * @author madison
 * @description
 * @date 2021/10/21 10:59
 */
public class LiveEncoder extends MessageToByteEncoder<LiveMessage> {


    @Override
    protected void encode(ChannelHandlerContext ctx, LiveMessage msg, ByteBuf out) throws Exception {
        out.writeByte(msg.getType());
        out.writeInt(msg.getLength());
        if (!StringUtil.isNullOrEmpty(msg.getContent())) {
            out.writeBytes(msg.getContent().getBytes(StandardCharsets.UTF_8));
        }
    }
}
